home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / mpeg / decoders.h < prev    next >
C/C++ Source or Header  |  1994-08-01  |  16KB  |  475 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. /*
  22.  * decoders.h
  23.  *
  24.  * This file contains the declarations of structures required for Huffman
  25.  * decoding
  26.  *
  27.  */
  28.  
  29. /* Include util.h for bit i/o parsing macros. */
  30.  
  31. #include "util.h"
  32.  
  33. /* Code for unbound values in decoding tables */
  34. #define ERROR -1
  35. #define DCT_ERROR 63
  36.  
  37. #define MACRO_BLOCK_STUFFING 34
  38. #define MACRO_BLOCK_ESCAPE 35
  39.  
  40. /* Two types of DCT Coefficients */
  41. #define DCT_COEFF_FIRST 0
  42. #define DCT_COEFF_NEXT 1
  43.  
  44. /* Special values for DCT Coefficients */
  45. #define END_OF_BLOCK 62
  46. #define ESCAPE 61
  47.  
  48. /* Structure for an entry in the decoding table of 
  49.  * macroblock_address_increment */
  50. typedef struct {
  51.   unsigned int value;       /* value for macroblock_address_increment */
  52.   int num_bits;             /* length of the Huffman code */
  53. } mb_addr_inc_entry;
  54.  
  55. /* Decoding table for macroblock_address_increment */
  56. extern mb_addr_inc_entry mb_addr_inc[2048];
  57.  
  58.  
  59. /* Structure for an entry in the decoding table of macroblock_type */
  60. typedef struct {
  61.   unsigned int mb_quant;              /* macroblock_quant */
  62.   unsigned int mb_motion_forward;     /* macroblock_motion_forward */
  63.   unsigned int mb_motion_backward;    /* macroblock_motion_backward */
  64.   unsigned int mb_pattern;            /* macroblock_pattern */
  65.   unsigned int mb_intra;              /* macroblock_intra */
  66.   int num_bits;                       /* length of the Huffman code */
  67. } mb_type_entry;
  68.  
  69. /* Decoding table for macroblock_type in predictive-coded pictures */
  70. extern mb_type_entry mb_type_P[64];
  71.  
  72. /* Decoding table for macroblock_type in bidirectionally-coded pictures */
  73. extern mb_type_entry mb_type_B[64];
  74.  
  75.  
  76. /* Structures for an entry in the decoding table of coded_block_pattern */
  77. typedef struct {
  78.   unsigned int cbp;            /* coded_block_pattern */
  79.   int num_bits;                /* length of the Huffman code */
  80. } coded_block_pattern_entry;
  81.  
  82. /* External declaration of coded block pattern table. */
  83.  
  84. extern coded_block_pattern_entry coded_block_pattern[512];
  85.  
  86.  
  87.  
  88. /* Structure for an entry in the decoding table of motion vectors */
  89. typedef struct {
  90.   int code;              /* value for motion_horizontal_forward_code,
  91.               * motion_vertical_forward_code, 
  92.               * motion_horizontal_backward_code, or
  93.               * motion_vertical_backward_code.
  94.               */
  95.   int num_bits;          /* length of the Huffman code */
  96. } motion_vectors_entry;
  97.  
  98.  
  99. /* Decoding table for motion vectors */
  100. extern motion_vectors_entry motion_vectors[2048];
  101.  
  102.  
  103. /* Structure for an entry in the decoding table of dct_dc_size */
  104. typedef struct {
  105.   unsigned int value;    /* value of dct_dc_size (luminance or chrominance) */
  106.   int num_bits;          /* length of the Huffman code */
  107. } dct_dc_size_entry;
  108.  
  109. /* External declaration of dct dc size lumiance table. */
  110.  
  111. extern dct_dc_size_entry dct_dc_size_luminance[128];
  112.  
  113. /* External declaration of dct dc size chrom table. */
  114.  
  115. extern dct_dc_size_entry dct_dc_size_chrominance[256];
  116.  
  117.  
  118. /* DCT coeff tables. */
  119.  
  120. #define RUN_MASK 0xfc00
  121. #define LEVEL_MASK 0x03f0
  122. #define NUM_MASK 0x000f
  123. #define RUN_SHIFT 10
  124. #define LEVEL_SHIFT 4
  125.  
  126. /* External declaration of dct coeff tables. */
  127.  
  128. extern unsigned short int dct_coeff_tbl_0[256];
  129. extern unsigned short int dct_coeff_tbl_1[16];
  130. extern unsigned short int dct_coeff_tbl_2[4];
  131. extern unsigned short int dct_coeff_tbl_3[4];
  132. extern unsigned short int dct_coeff_next[256];
  133. extern unsigned short int dct_coeff_first[256];
  134.  
  135. #define DecodeDCTDCSizeLum(macro_val)                    \
  136. {                                                    \
  137.   unsigned int index;                                \
  138.                                                      \
  139.   show_bits7(index);                              \
  140.                                                      \
  141.   macro_val = dct_dc_size_luminance[index].value;       \
  142.                                                      \
  143.   flush_bits(dct_dc_size_luminance[index].num_bits); \
  144. }
  145.  
  146. #define DecodeDCTDCSizeChrom(macro_val)                      \
  147. {                                                        \
  148.   unsigned int index;                                    \
  149.                                                          \
  150.   show_bits8(index);                                  \
  151.                                                          \
  152.   macro_val = dct_dc_size_chrominance[index].value;         \
  153.                                                          \
  154.   flush_bits(dct_dc_size_chrominance[index].num_bits);   \
  155. }
  156.  
  157. #define DecodeDCTCoeff(dct_coeff_tbl, run, level)            \
  158. {                                    \
  159.   unsigned int temp, index;                        \
  160.   unsigned int value, next32bits, flushed;                \
  161.                                     \
  162.   /*                                    \
  163.    * Grab the next 32 bits and use it to improve performance of        \
  164.    * getting the bits to parse. Thus, calls are translated as:        \
  165.    *                                    \
  166.    *    show_bitsX  <-->   next32bits >> (32-X)                \
  167.    *    get_bitsX   <-->   val = next32bits >> (32-flushed-X);        \
  168.    *               flushed += X;                \
  169.    *               next32bits &= bitMask[flushed];        \
  170.    *    flush_bitsX <-->   flushed += X;                \
  171.    *               next32bits &= bitMask[flushed];        \
  172.    *                                    \
  173.    * I've streamlined the code a lot, so that we don't have to mask    \
  174.    * out the low order bits and a few of the extra adds are removed.    \
  175.    */                                    \
  176.   show_bits32(next32bits);                        \
  177.                                     \
  178.   /* show_bits8(index); */                        \
  179.   index = next32bits >> 24;                        \
  180.                                     \
  181.   if (index > 3) {                            \
  182.     value = dct_coeff_tbl[index];                    \
  183.     run = value >> RUN_SHIFT;                        \
  184.     if (run != END_OF_BLOCK) {                        \
  185.       /* num_bits = (value & NUM_MASK) + 1; */                \
  186.       /* flush_bits(num_bits); */                    \
  187.       if (run != ESCAPE) {                        \
  188.      /* get_bits1(value); */                    \
  189.      /* if (value) level = -level; */                \
  190.      flushed = (value & NUM_MASK) + 2;                \
  191.          level = (value & LEVEL_MASK) >> LEVEL_SHIFT;            \
  192.      value = next32bits >> (32-flushed);                \
  193.      value &= 0x1;                            \
  194.      if (value) level = -level;                    \
  195.      /* next32bits &= ((~0) >> flushed);  last op before update */    \
  196.        }                                \
  197.        else {    /* run == ESCAPE */                    \
  198.      /* Get the next six into run, and next 8 into temp */        \
  199.          /* get_bits14(temp); */                    \
  200.      flushed = (value & NUM_MASK) + 1;                \
  201.      temp = next32bits >> (18-flushed);                \
  202.      /* Normally, we'd ad 14 to flushed, but I've saved a few    \
  203.       * instr by moving the add below */                \
  204.      temp &= 0x3fff;                        \
  205.      run = temp >> 8;                        \
  206.      temp &= 0xff;                            \
  207.      if (temp == 0) {                        \
  208.             /* get_bits8(level); */                    \
  209.         level = next32bits >> (10-flushed);                \
  210.         level &= 0xff;                        \
  211.         flushed += 22;                        \
  212.          assert(level >= 128);                    \
  213.      } else if (temp != 128) {                    \
  214.         /* Grab sign bit */                        \
  215.         flushed += 14;                        \
  216.         level = ((int) (temp << 24)) >> 24;                \
  217.      } else {                            \
  218.             /* get_bits8(level); */                    \
  219.         level = next32bits >> (10-flushed);                \
  220.         level &= 0xff;                        \
  221.         flushed += 22;                        \
  222.         level = level - 256;                    \
  223.         assert(level <= -128 && level >= -255);            \
  224.      }                                \
  225.        }                                \
  226.        /* Update bitstream... */                    \
  227.        flush_bits(flushed);                        \
  228.        assert (flushed <= 32);                        \
  229.     }                                    \
  230.   }                                    \
  231.   else {                                \
  232.     if (index == 2) {                             \
  233.       /* show_bits10(index); */                        \
  234.       index = next32bits >> 22;                        \
  235.       value = dct_coeff_tbl_2[index & 3];                \
  236.     }                                    \
  237.     else if (index == 3) {                         \
  238.       /* show_bits10(index); */                        \
  239.       index = next32bits >> 22;                        \
  240.       value = dct_coeff_tbl_3[index & 3];                \
  241.     }                                    \
  242.     else if (index) {    /* index == 1 */                \
  243.       /* show_bits12(index); */                        \
  244.       index = next32bits >> 20;                        \
  245.       value = dct_coeff_tbl_1[index & 15];                \
  246.     }                                    \
  247.     else {   /* index == 0 */                        \
  248.       /* show_bits16(index); */                        \
  249.       index = next32bits >> 16;                        \
  250.       value = dct_coeff_tbl_0[index & 255];                \
  251.     }                                    \
  252.     run = value >> RUN_SHIFT;                        \
  253.     level = (value & LEVEL_MASK) >> LEVEL_SHIFT;            \
  254.                                     \
  255.     /*                                    \
  256.      * Fold these operations together to make it fast...        \
  257.      */                                    \
  258.     /* num_bits = (value & NUM_MASK) + 1; */                \
  259.     /* flush_bits(num_bits); */                        \
  260.     /* get_bits1(value); */                        \
  261.     /* if (value) level = -level; */                    \
  262.                                     \
  263.     flushed = (value & NUM_MASK) + 2;                    \
  264.     value = next32bits >> (32-flushed);                    \
  265.     value &= 0x1;                            \
  266.     if (value) level = -level;                        \
  267.                                     \
  268.     /* Update bitstream ... */                        \
  269.     flush_bits(flushed);                        \
  270.     assert (flushed <= 32);                        \
  271.   }                                    \
  272. }
  273.  
  274. #define DecodeDCTCoeffFirst(runval, levelval)         \
  275. {                                                     \
  276.   DecodeDCTCoeff(dct_coeff_first, runval, levelval);  \
  277. }          
  278.  
  279. #define DecodeDCTCoeffNext(runval, levelval)          \
  280. {                                                     \
  281.   DecodeDCTCoeff(dct_coeff_next, runval, levelval);   \
  282. }
  283.  
  284. /*
  285.  *--------------------------------------------------------------
  286.  *
  287.  * DecodeMBAddrInc --
  288.  *
  289.  *      Huffman Decoder for macro_block_address_increment; the location
  290.  *      in which the result will be placed is being passed as argument.
  291.  *      The decoded value is obtained by doing a table lookup on
  292.  *      mb_addr_inc.
  293.  *
  294.  * Results:
  295.  *      The decoded value for macro_block_address_increment or ERROR
  296.  *      for unbound values will be placed in the location specified.
  297.  *
  298.  * Side effects:
  299.  *      Bit stream is irreversibly parsed.
  300.  *
  301.  *--------------------------------------------------------------
  302.  */
  303. #define DecodeMBAddrInc(val)                \
  304. {                            \
  305.     unsigned int index;                    \
  306.     show_bits11(index);                    \
  307.     val = mb_addr_inc[index].value;            \
  308.     flush_bits(mb_addr_inc[index].num_bits);        \
  309. }
  310.  
  311. /*
  312.  *--------------------------------------------------------------
  313.  *
  314.  * DecodeMotionVectors --
  315.  *
  316.  *      Huffman Decoder for the various motion vectors, including
  317.  *      motion_horizontal_forward_code, motion_vertical_forward_code,
  318.  *      motion_horizontal_backward_code, motion_vertical_backward_code.
  319.  *      Location where the decoded result will be placed is being passed
  320.  *      as argument. The decoded values are obtained by doing a table
  321.  *      lookup on motion_vectors.
  322.  *
  323.  * Results:
  324.  *      The decoded value for the motion vector or ERROR for unbound
  325.  *      values will be placed in the location specified.
  326.  *
  327.  * Side effects:
  328.  *      Bit stream is irreversibly parsed.
  329.  *
  330.  *--------------------------------------------------------------
  331.  */
  332.  
  333. #define DecodeMotionVectors(value)            \
  334. {                            \
  335.   unsigned int index;                    \
  336.   show_bits11(index);                    \
  337.   value = motion_vectors[index].code;            \
  338.   flush_bits(motion_vectors[index].num_bits);        \
  339. }
  340. /*
  341.  *--------------------------------------------------------------
  342.  *
  343.  * DecodeMBTypeB --
  344.  *
  345.  *      Huffman Decoder for macro_block_type in bidirectionally-coded
  346.  *      pictures;locations in which the decoded results: macroblock_quant,
  347.  *      macroblock_motion_forward, macro_block_motion_backward,
  348.  *      macroblock_pattern, macro_block_intra, will be placed are
  349.  *      being passed as argument. The decoded values are obtained by
  350.  *      doing a table lookup on mb_type_B.
  351.  *
  352.  * Results:
  353.  *      The various decoded values for macro_block_type in
  354.  *      bidirectionally-coded pictures or ERROR for unbound values will
  355.  *      be placed in the locations specified.
  356.  *
  357.  * Side effects:
  358.  *      Bit stream is irreversibly parsed.
  359.  *
  360.  *--------------------------------------------------------------
  361.  */
  362. #define DecodeMBTypeB(quant, motion_fwd, motion_bwd, pat, intra)    \
  363. {                                    \
  364.   unsigned int index;                            \
  365.                                     \
  366.   show_bits6(index);                            \
  367.                                     \
  368.   quant = mb_type_B[index].mb_quant;                    \
  369.   motion_fwd = mb_type_B[index].mb_motion_forward;            \
  370.   motion_bwd = mb_type_B[index].mb_motion_backward;            \
  371.   pat = mb_type_B[index].mb_pattern;                    \
  372.   intra = mb_type_B[index].mb_intra;                    \
  373.   flush_bits(mb_type_B[index].num_bits);                \
  374. }
  375. /*
  376.  *--------------------------------------------------------------
  377.  *
  378.  * DecodeMBTypeI --
  379.  *
  380.  *      Huffman Decoder for macro_block_type in intra-coded pictures;
  381.  *      locations in which the decoded results: macroblock_quant,
  382.  *      macroblock_motion_forward, macro_block_motion_backward,
  383.  *      macroblock_pattern, macro_block_intra, will be placed are
  384.  *      being passed as argument.
  385.  *
  386.  * Results:
  387.  *      The various decoded values for macro_block_type in intra-coded
  388.  *      pictures or ERROR for unbound values will be placed in the
  389.  *      locations specified.
  390.  *
  391.  * Side effects:
  392.  *      Bit stream is irreversibly parsed.
  393.  *
  394.  *--------------------------------------------------------------
  395.  */
  396. #define DecodeMBTypeI(quant, motion_fwd, motion_bwd, pat, intra)    \
  397. {                                    \
  398.   unsigned int index;                            \
  399.   static int quantTbl[4] = {ERROR, 1, 0, 0};                \
  400.                                     \
  401.   show_bits2(index);                            \
  402.                                     \
  403.   motion_fwd = 0;                            \
  404.   motion_bwd = 0;                            \
  405.   pat = 0;                                \
  406.   intra = 1;                                \
  407.   quant = quantTbl[index];                        \
  408.   if (index) {                                \
  409.     flush_bits (1 + quant);                        \
  410.   }                                    \
  411. }
  412. /*
  413.  *--------------------------------------------------------------
  414.  *
  415.  * DecodeMBTypeP --
  416.  *
  417.  *      Huffman Decoder for macro_block_type in predictive-coded pictures;
  418.  *      locations in which the decoded results: macroblock_quant,
  419.  *      macroblock_motion_forward, macro_block_motion_backward,
  420.  *      macroblock_pattern, macro_block_intra, will be placed are
  421.  *      being passed as argument. The decoded values are obtained by
  422.  *      doing a table lookup on mb_type_P.
  423.  *
  424.  * Results:
  425.  *      The various decoded values for macro_block_type in
  426.  *      predictive-coded pictures or ERROR for unbound values will be
  427.  *      placed in the locations specified.
  428.  *
  429.  * Side effects:
  430.  *      Bit stream is irreversibly parsed.
  431.  *
  432.  *--------------------------------------------------------------
  433.  */
  434. #define DecodeMBTypeP(quant, motion_fwd, motion_bwd, pat, intra)    \
  435. {                                    \
  436.   unsigned int index;                            \
  437.                                     \
  438.   show_bits6(index);                            \
  439.                                     \
  440.   quant = mb_type_P[index].mb_quant;                    \
  441.   motion_fwd = mb_type_P[index].mb_motion_forward;            \
  442.   motion_bwd = mb_type_P[index].mb_motion_backward;            \
  443.   pat = mb_type_P[index].mb_pattern;                    \
  444.   intra = mb_type_P[index].mb_intra;                    \
  445.                                     \
  446.   flush_bits(mb_type_P[index].num_bits);                \
  447. }
  448. /*
  449.  *--------------------------------------------------------------
  450.  *
  451.  * DecodeCBP --
  452.  *
  453.  *      Huffman Decoder for coded_block_pattern; location in which the
  454.  *      decoded result will be placed is being passed as argument. The
  455.  *      decoded values are obtained by doing a table lookup on
  456.  *      coded_block_pattern.
  457.  *
  458.  * Results:
  459.  *      The decoded value for coded_block_pattern or ERROR for unbound
  460.  *      values will be placed in the location specified.
  461.  *
  462.  * Side effects:
  463.  *      Bit stream is irreversibly parsed.
  464.  *
  465.  *--------------------------------------------------------------
  466.  */
  467. #define DecodeCBP(coded_bp)                        \
  468. {                                    \
  469.   unsigned int index;                            \
  470.                                     \
  471.   show_bits9(index);                            \
  472.   coded_bp = coded_block_pattern[index].cbp;                \
  473.   flush_bits(coded_block_pattern[index].num_bits);            \
  474. }
  475.